package com.panasonic.web.controller.cms;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.panasonic.common.annotation.Log;
import com.panasonic.common.core.controller.BaseController;
import com.panasonic.common.core.domain.AjaxResult;
import com.panasonic.common.core.page.TableDataInfo;
import com.panasonic.common.enums.BusinessType;
import com.panasonic.common.utils.ExcelUtil;
import com.panasonic.common.utils.bean.ListBeanUtil;
import com.panasonic.web.domain.cms.CmsUser;
import com.panasonic.web.listener.CmsUserDataListener;
import com.panasonic.web.service.ICmsUserService;
import com.panasonic.web.util.CmsSecurityUtils;
import com.panasonic.web.util.JavaMailService;
import com.panasonic.web.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.MessagingException;
import java.io.IOException;
import java.util.List;

/**
 * 用户Controller
 *
 * @author lwf
 * @date 2021-11-13
 */
@RestController
@RequestMapping("/cms/user")
@Api(value = "CmsUser", tags = "用户")
public class CmsUserController extends BaseController {
    @Autowired
    private ICmsUserService cmsUserService;

    @Autowired
    private JavaMailService javaMailService;
    /**
     * 查询用户列表
     */
    @ApiOperation(value = "查询")
    @GetMapping("/list")
    public TableDataInfo list(CmsUser cmsUser) {

        startPage();
        List<CmsUser> list = cmsUserService.selectCmsUserList(cmsUser);
        List<CmsUserVo> resultList =
                ListBeanUtil.copyListProperties(list, CmsUserVo::new);
        TableDataInfo dataTable = getDataTable(list);
        dataTable.setRows(resultList);
        return dataTable;
    }

    /**
     * 获取用户详细信息
     */
    @ApiOperation(value = "获取用户详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        CmsUser cmsUser = cmsUserService.selectCmsUserById(id);
        CmsUserVo cmsUserVO = new CmsUserVo();
        BeanUtil.copyProperties(cmsUser, cmsUserVO);
        return AjaxResult.success(cmsUser);
    }

    /**
     * 新增用户
     */
    @ApiOperation(value = "新增用户")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmsUser cmsUser,boolean send) {
        try{
            return toAjax(cmsUserService.insertCmsUser(cmsUser,send));

        }catch(DuplicateKeyException e ){
            return AjaxResult.error("用户名已存在,请修改");
        }
    }

    /**
     * 修改用户
     */
    @ApiOperation(value = "修改用户")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmsUser cmsUser,boolean send) {
        return toAjax(cmsUserService.updateCmsUser(cmsUser,send));
    }

    /**
     * 删除用户
     */
    @ApiOperation(value = "删除用户")
    @Log(title = "用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(cmsUserService.deleteCmsUserByIds(ids));
    }

    @ApiOperation(value = "修改状态，1 停用 0 正常")
    @Log(title = "修改状态", businessType = BusinessType.UPDATE)
    @PostMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody @Validated CmsBatchStatusVo cmsBatchStatusVo) {
        if (cmsBatchStatusVo.getStatus().equals("0") || cmsBatchStatusVo.getStatus().equals("1")) {
            if (CmsSecurityUtils.getCmsLoginUser().getUser().getType() > 0) {
                return AjaxResult.error("没有权限修改");
            }
            return toAjax(cmsUserService.changeStatus(cmsBatchStatusVo));
        } else {
            return AjaxResult.error("状态错误");
        }
    }

    @ApiOperation(value = "批量导入")
    @Log(title = "批量导入", businessType = BusinessType.INSERT)
    @PostMapping("upload")
    @ResponseBody
    public AjaxResult upload(MultipartFile file, String permissionIds, Integer type, Integer pan, Long panId,boolean send) throws IOException {
        EasyExcel.read(file.getInputStream(), CmsUserImportVo.class, new CmsUserDataListener(cmsUserService, javaMailService, permissionIds,type,pan,panId,send)).sheet().doRead();
        return AjaxResult.success("导入成功");
    }


    @ApiOperation(value = "忘记密码")
    @Log(title = "忘记密码", businessType = BusinessType.UPDATE)
    @PostMapping("forgetPassword")
    @ResponseBody
    public AjaxResult forgetPassword(@RequestBody ForgetPasswordVo forgetPasswordVo) throws MessagingException {
        cmsUserService.forgetPassword(forgetPasswordVo);
        return AjaxResult.success("已经发送验证码到邮箱");
    }

    @ApiOperation(value = "修改密码")
    @Log(title = "修改密码", businessType = BusinessType.UPDATE)
    @PostMapping("resetpassword")
    @ResponseBody
    public AjaxResult reset(@RequestBody ResetPasswordVo resetPasswordVo) {
        cmsUserService.resetPassword(resetPasswordVo);
        return AjaxResult.success("修改成功");
    }


    @Log(title = "用户导出", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CmsUser cmsUser)
    {
        List<CmsUser> list = cmsUserService.selectCmsUserList(cmsUser);
        ExcelUtil<CmsUser> util = new ExcelUtil<CmsUser>(CmsUser.class);
        String fileName = "管理员数据";
        if(cmsUser.getType().equals(1)){
            fileName = "维修店数据";
        }else if(cmsUser.getType().equals(2)){
            fileName = "经销商数据";
        }
        return util.exportExcel(list, fileName);
    }
    


}
